package com.example.springboot_sdse_air.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot_sdse_air.common.Result;
import com.example.springboot_sdse_air.entity.Pdf;
import com.example.springboot_sdse_air.service.PdfService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能：
 * 作者：王彬 202200111149
 * 日期：2025-04-21 16:05
 */
@CrossOrigin
@RestController
@RequestMapping("/pdf")
public class PdfController {

    @Resource
    PdfService pdfService;

    /**
     * 新增PDF
     */
    @PostMapping("/add")
    public Result add(@RequestBody Pdf pdf) {
        try {
            pdfService.save(pdf);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 删除PDF
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        try {
            boolean result = pdfService.deleteById(id);
            if (result) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败，文件不存在");
            }
        } catch (Exception e) {
            return Result.error("删除失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除PDF
     */
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        try {
            boolean result = pdfService.batchDelete(ids);
            if (result) {
                return Result.success("批量删除成功");
            } else {
                return Result.error("批量删除失败");
            }
        } catch (Exception e) {
            return Result.error("批量删除失败: " + e.getMessage());
        }
    }

    /**
     * 根据用户ID查询全部PDF
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        QueryWrapper<Pdf> queryWrapper = new QueryWrapper<Pdf>().orderByDesc("upload_time");
        queryWrapper.eq("uid", id);
        List<Pdf> pdfList = pdfService.list(queryWrapper);
        return Result.success(pdfList);
    }

    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam Integer uid) {
        QueryWrapper<Pdf> queryWrapper = new QueryWrapper<Pdf>().orderByDesc("upload_time");
        queryWrapper.eq("uid", uid);

        Page<Pdf> page = pdfService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    /**
     * 多条件分页模糊查询PDF(根据URL参数查询)
     */
    @GetMapping("/selectByPageAmbiguous")
    public Result selectByPageAmbiguous(@RequestParam Integer PageNum,
                                        @RequestParam Integer PageSize,
                                        @RequestParam Integer uid,
                                        @RequestParam String pdf_name) {
        QueryWrapper<Pdf> queryWrapper = new QueryWrapper<Pdf>().orderByDesc("upload_time");
        queryWrapper.eq("uid", uid);
        queryWrapper.like(StrUtil.isNotBlank(pdf_name), "name", pdf_name);

        Page<Pdf> page = pdfService.page(new Page<>(PageNum, PageSize), queryWrapper);
        return Result.success(page);
    }

}
